// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.core.stash;

import java.util.ArrayList;
import se.krka.kahlua.j2se.KahluaTableImpl;
import se.krka.kahlua.vm.KahluaTable;
import se.krka.kahlua.vm.KahluaTableIterator;
import zombie.core.Translator;
import zombie.debug.DebugLog;
import zombie.scripting.ScriptManager;

public final class Stash {
    public String name;
    public String type;
    public String item;
    public String customName;
    public int buildingX;
    public int buildingY;
    public String spawnTable;
    public ArrayList<StashAnnotation> annotations;
    public boolean spawnOnlyOnZed;
    public int minDayToSpawn = -1;
    public int maxDayToSpawn = -1;
    public int minTrapToSpawn = -1;
    public int maxTrapToSpawn = -1;
    public int zombies;
    public ArrayList<StashContainer> containers;
    public int barricades;

    public Stash(String _name) {
        this.name = _name;
    }

    public void load(KahluaTableImpl kahluaTableImpl0) {
        this.type = kahluaTableImpl0.rawgetStr("type");
        this.item = kahluaTableImpl0.rawgetStr("item");
        StashBuilding stashBuilding = new StashBuilding(this.name, kahluaTableImpl0.rawgetInt("buildingX"), kahluaTableImpl0.rawgetInt("buildingY"));
        StashSystem.possibleStashes.add(stashBuilding);
        this.buildingX = stashBuilding.buildingX;
        this.buildingY = stashBuilding.buildingY;
        this.spawnTable = kahluaTableImpl0.rawgetStr("spawnTable");
        this.customName = Translator.getText(kahluaTableImpl0.rawgetStr("customName"));
        this.zombies = kahluaTableImpl0.rawgetInt("zombies");
        this.barricades = kahluaTableImpl0.rawgetInt("barricades");
        this.spawnOnlyOnZed = kahluaTableImpl0.rawgetBool("spawnOnlyOnZed");
        String string0 = kahluaTableImpl0.rawgetStr("daysToSpawn");
        if (string0 != null) {
            String[] string1 = string0.split("-");
            if (string1.length == 2) {
                this.minDayToSpawn = Integer.parseInt(string1[0]);
                this.maxDayToSpawn = Integer.parseInt(string1[1]);
            } else {
                this.minDayToSpawn = Integer.parseInt(string1[0]);
            }
        }

        String string2 = kahluaTableImpl0.rawgetStr("traps");
        if (string2 != null) {
            String[] string3 = string2.split("-");
            if (string3.length == 2) {
                this.minTrapToSpawn = Integer.parseInt(string3[0]);
                this.maxTrapToSpawn = Integer.parseInt(string3[1]);
            } else {
                this.minTrapToSpawn = Integer.parseInt(string3[0]);
                this.maxTrapToSpawn = this.minTrapToSpawn;
            }
        }

        KahluaTable kahluaTable0 = (KahluaTable)kahluaTableImpl0.rawget("containers");
        if (kahluaTable0 != null) {
            this.containers = new ArrayList();
            KahluaTableIterator kahluaTableIterator0 = kahluaTable0.iterator();

            while (kahluaTableIterator0.advance()) {
                KahluaTableImpl kahluaTableImpl1 = (KahluaTableImpl)kahluaTableIterator0.getValue();
                StashContainer stashContainer = new StashContainer(
                    kahluaTableImpl1.rawgetStr("room"), kahluaTableImpl1.rawgetStr("containerSprite"), kahluaTableImpl1.rawgetStr("containerType")
                );
                stashContainer.contX = kahluaTableImpl1.rawgetInt("contX");
                stashContainer.contY = kahluaTableImpl1.rawgetInt("contY");
                stashContainer.contZ = kahluaTableImpl1.rawgetInt("contZ");
                stashContainer.containerItem = kahluaTableImpl1.rawgetStr("containerItem");
                if (stashContainer.containerItem != null && ScriptManager.instance.getItem(stashContainer.containerItem) == null) {
                    DebugLog.General.error("Stash containerItem \"%s\" doesn't exist.", stashContainer.containerItem);
                }

                this.containers.add(stashContainer);
            }
        }

        if ("Map".equals(this.type)) {
            KahluaTableImpl kahluaTableImpl2 = (KahluaTableImpl)kahluaTableImpl0.rawget("annotations");
            if (kahluaTableImpl2 != null) {
                this.annotations = new ArrayList();
                KahluaTableIterator kahluaTableIterator1 = kahluaTableImpl2.iterator();

                while (kahluaTableIterator1.advance()) {
                    KahluaTable kahluaTable1 = (KahluaTable)kahluaTableIterator1.getValue();
                    StashAnnotation stashAnnotation = new StashAnnotation();
                    stashAnnotation.fromLua(kahluaTable1);
                    this.annotations.add(stashAnnotation);
                }
            }
        }
    }

    public String getName() {
        return this.name;
    }

    public String getItem() {
        return this.item;
    }

    public int getBuildingX() {
        return this.buildingX;
    }

    public int getBuildingY() {
        return this.buildingY;
    }
}
